//
//  ST_PushPeculiarityGuidanceViewController.swift
//  btvideo
//
//  Created by mac on 2020/4/7.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import SwiftyUserDefaults

class ST_PushPeculiarityGuidanceViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(root_view_width)
            make.height.equalTo(root_view_height)
        }
        
        root_view.addSubview(scrollView)
        
        scrollView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(134)
        }
        
        
        
        scrollView.addSubview(contetnView)
        contetnView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(4*root_view_width)
            make.height.equalTo(134)
        }
        
        root_view.addSubview(pageControl)
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(scrollView.snp.bottom).offset(5)
            make.height.equalTo(5+10)
        }
        
        root_view.addSubview(titleLabel)
        root_view.addSubview(content_label)
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(scrollView.snp.bottom).offset(35)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
        }
        
        root_view.addSubview(next_btn)
        next_btn.snp.makeConstraints { (make) in
            make.width.equalTo(160)
            make.height.equalTo(35)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-25)
        }
        
        root_view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.equalTo(26)
            make.height.equalTo(26)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
        }
        
        
        bind_data()
    }
    
    fileprivate lazy var data_array = [ST_PeculiarityGuidanceModel.init(imageName: "什么是特质", title: "什么是特质", content: "特质是你对异性展现出的吸引力、魅力"),ST_PeculiarityGuidanceModel.init(imageName: "闪光点分类", title: "用特质交友", content: "我们不只看颜值，更看重有趣的灵魂"),ST_PeculiarityGuidanceModel.init(imageName: "被心仪对象发现", title: "曝光与推荐", content: "拥有特质就能在特质版块被发现"),ST_PeculiarityGuidanceModel.init(imageName: "100%真实有效", title: "严格审核保证质量", content: "你的优秀特质能收获超多喜欢")]
    
    fileprivate func bind_data(){
        
       var lastImageView:UIImageView?
       
       for item in data_array{
           
           let imageView = UIImageView.init(image: UIImage.init(named: item.imageName)!)
           
           contetnView.addSubview(imageView)
           imageView.snp.makeConstraints { (make) in
               if let last = lastImageView{
                   make.left.equalTo(last.snp.right).offset(35*2)
               }else{
                   make.left.equalToSuperview().offset(35)
               }
               make.top.height.equalToSuperview()
               make.width.equalTo(230)
           }
           
           lastImageView = imageView
       }
        
        
        
        titleLabel.text = data_array.first!.title
        
        content_label.text = data_array.first!.content
        
    }
    
    
    
    @objc var root_view_height = 320.cgFloat
    
    @objc var root_view_width = 300.cgFloat
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 20
//        control.borderColor = Color.init(hexString: "#979797")
//        control.borderWidth = 1
        return control
    }()
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.backgroundColor = Color.white
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        control.isPagingEnabled = true
//        control.delegate = self
        control.isScrollEnabled = false
        return control
    }()
    
    fileprivate lazy var contetnView:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    fileprivate var currentPage = 0
    
    fileprivate lazy var pageControl:UIPageControl = {
        let control = UIPageControl()
        control.numberOfPages = 4
        control.currentPage = 0
        control.pageIndicatorTintColor = UIColor.init(hex: 0xE7E7E7)
        control.currentPageIndicatorTintColor = UIColor.init(hex: 0xFEC207)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()

    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var next_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.cornerRadius = 34/2.0
        control.setTitleForAllStates("下一步")
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(next_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("特质引导_icon_关闭")!)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()
    
}

extension ST_PushPeculiarityGuidanceViewController{
    
    @objc fileprivate func cancel_handle(){
        
        Defaults[.PushPeculiarityGuidance] = false
        
        goback_previous_controller()
    }
    
    @objc fileprivate func next_handle(){
         
        if currentPage == 3{
            
            self.dismiss(animated: true) {
                
                if ST_BTUserInfoManager.shared.confined_to_ticket(){
                    
                    let vc = ST_TicketViewController()
                    
                    vc.from = .mainIndexConfined
                    
                    GlobalPresent(toController: vc)
                    
                    return
                }
                
                GlobalPresent(toController: UINavigationController.create_hiding_bar_navigation_controller(rootViewController: ST_PushPeculiarityViewController()))
                
             
                
            }
            return
        }
        self.next_btn.isEnabled = false
        
        currentPage += 1
        
        UIView.animate(withDuration: 0, animations: {
            
            self.scrollView.setContentOffset(CGPoint.init(x: self.root_view_width*self.currentPage.cgFloat, y: 0), animated: true)
            
        }) { (f) in
            
            self.next_btn.isEnabled = true
            
            if let item = self.data_array[safe:self.currentPage]{
                
                self.titleLabel.text = item.title
                
                self.content_label.text = item.content
            }
            
            self.pageControl.currentPage = self.currentPage
            
            if self.currentPage == 3{
//                去添加
                 if FACE_AVATAE_STATE == 3{
                    
                    self.next_btn.setTitleForAllStates("去添加")
                    
                 }else{
                    self.next_btn.setTitleForAllStates("去发布")
                }
                
                self.close_btn.isHidden = false
                
            }
        }
    }
    
    
    func next_detec_face_handle(){
        
        
        if FACE_AVATAE_STATE == 1{
            
            DetectFaceHandle()
            
        }else{
            
            let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: ST_MyPhotoViewController())
            
            GlobalPresent(toController: nav)
            
        }
        
    }
    
//    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
//
//
//        let currPage = (scrollView.contentOffset.x/self.root_view_width).int
//
//
//    }
    
    
}


fileprivate struct ST_PeculiarityGuidanceModel {
    var imageName:String
    var title:String
    var content:String
}
